package com.iflytek.universitymanagement.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.iflytek.universitymanagement.config.Result;
import com.iflytek.universitymanagement.config.RoleEnum;
import com.iflytek.universitymanagement.domain.Account;
import com.iflytek.universitymanagement.domain.Admin;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.exception.ServiceException;
import com.iflytek.universitymanagement.service.AdminService;
import com.iflytek.universitymanagement.service.StudentService;
import com.iflytek.universitymanagement.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class WebController {
    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    AdminService adminService;

    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        if (ObjectUtil.isEmpty(account.getName()) || ObjectUtil.isEmpty(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error("数据输入错误");
        }
        if (RoleEnum.STUDENT.name().equals(account.getRole())) {
            account = studentService.login(account);
        }
        if (RoleEnum.TEACHER.name().equals(account.getRole())) {
            account = teacherService.login(account);
        }
        return Result.success(account);
    }


        @PostMapping("/adminLogin")
    public Result login(@RequestBody Admin admin) {
        if (StrUtil.isBlank(admin.getName()) || StrUtil.isBlank(admin.getPassword())) {
            return Result.error("数据输入错误");
        }
            admin = adminService.login(admin);
        return Result.success(admin);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        if (StrUtil.isBlank(account.getName()) || StrUtil.isBlank(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            throw new ServiceException("输入不合法");
        }
        if (RoleEnum.STUDENT.name().equals(account.getRole())) {
            studentService.register(account);
        } else if (RoleEnum.TEACHER.name().equals(account.getRole())) {
            teacherService.register(account);
        } else {
            throw new ServiceException("无效的角色");
        }
        return Result.success();
    }


    @PostMapping("/adminRegister")
    public Result register(@RequestBody Admin admin) {
        if (StrUtil.isBlank(admin.getName()) || StrUtil.isBlank(admin.getPassword())) {
            throw new ServiceException("输入不合法");
        }
        if (admin.getName().length() > 10 || admin.getPassword().length() > 20) {
            throw new ServiceException("长度过长");
        }
        admin = adminService.register(admin);
        return Result.success(admin);

    }


    @PutMapping("/password")
    public Result password(@RequestBody Student student) {
        if (StrUtil.isBlank(student.getName()) || StrUtil.isBlank(student.getContact())) {
            throw new ServiceException("数据输入不合法");
        }
        studentService.resetPassword(student);
        return Result.success(student);

    }
}
